package com.ruoyi.video.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.video.domain.VideoProgress;
import com.ruoyi.video.service.IVideoProgressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 观看进度Controller
 *
 * @author ruoyi
 * @date 2022-02-08
 */
@RestController
@RequestMapping("/video/progress")
public class VideoProgressController extends BaseController {
    @Autowired
    private IVideoProgressService videoProgressService;

    /**
     * 查询观看进度列表
     */
    @PreAuthorize("@ss.hasPermi('video:progress:list')")
    @GetMapping("/list")
    public TableDataInfo list(VideoProgress videoProgress) {
        startPage();
        List<VideoProgress> list = videoProgressService.selectVideoProgressList(videoProgress);
        return getDataTable(list);
    }

    /**
     * 导出观看进度列表
     */
    @PreAuthorize("@ss.hasPermi('video:progress:export')")
    @Log(title = "观看进度", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VideoProgress videoProgress) {
        List<VideoProgress> list = videoProgressService.selectVideoProgressList(videoProgress);
        ExcelUtil<VideoProgress> util = new ExcelUtil<VideoProgress>(VideoProgress.class);
        util.exportExcel(response, list, "观看进度数据");
    }

    /**
     * 获取观看进度详细信息
     */
    @PreAuthorize("@ss.hasPermi('video:progress:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(videoProgressService.selectVideoProgressById(id));
    }

    /**
     * 新增观看进度
     */
    @PreAuthorize("@ss.hasPermi('video:progress:add')")
    @Log(title = "观看进度", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VideoProgress videoProgress) {
        if (StringUtils.isEmpty(videoProgress.getVideoId()) || Objects.isNull(videoProgress.getProgress())) {
            return AjaxResult.noHaveParam();
        }
        int row = videoProgressService.deleteVideoProgressByVideoId(videoProgress.getVideoId());
        if (StringUtils.isEmpty(videoProgress.getId())) videoProgress.setId(IdUtils.fastUUID());
        row += videoProgressService.insertVideoProgress(videoProgress);
        return toAjax(row);
    }

    /**
     * 修改观看进度
     */
    @PreAuthorize("@ss.hasPermi('video:progress:edit')")
    @Log(title = "观看进度", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VideoProgress videoProgress) {
        return toAjax(videoProgressService.updateVideoProgress(videoProgress));
    }

    /**
     * 删除观看进度
     */
    @PreAuthorize("@ss.hasPermi('video:progress:remove')")
    @Log(title = "观看进度", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(videoProgressService.deleteVideoProgressByIds(ids));
    }
}
